//
//  CountrySelectView.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/28.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class CountrySelectView: UIView {

    let picker = UIPickerView()
    let contentView = UIView()
    var countryArray: [CountryModel] = []
    var selectCountry: (String) -> () = {_ in}
    let contentViewHeight: CGFloat = 290 + UIApplication.shared.bottomSafeDistance
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tap(_:))))
        
        contentView.config
            .backgroundColor(.white)
            .addTo(self)
            .makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.height.equalTo(contentViewHeight)
                make.bottom.equalTo(contentViewHeight)
        }
        
        let toolBar = UIView().config
            .backgroundColor(.white)
            .addTo(contentView)
            .makeConstraints { (make) in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(50)
        }.view
        
        UIButton().config
            .title("确定".localized)
            .titleColor(APPConfig.baseColor)
            .addTarget(self, action: #selector(confirm))
            .addTo(toolBar)
            .makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.top.equalToSuperview()
                make.right.equalTo(-10)
        }
        
        UIButton().config
            .title("取消".localized)
            .titleColor(#colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1))
            .addTarget(self, action: #selector(cancel))
            .addTo(toolBar)
            .makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.top.equalToSuperview()
                make.left.equalTo(10)
        }
        
        UIView().config
            .backgroundColor(#colorLiteral(red: 0.9772258401, green: 0.9772258401, blue: 0.9772258401, alpha: 1))
            .addTo(toolBar)
            .makeConstraints { (make) in
                make.bottom.left.right.equalToSuperview()
                make.height.equalTo(1)
        }
        
        picker.config.delegate(self)
            .dataSource(self)
            .addTo(contentView)
            .makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.top.equalTo(toolBar.snp.bottom)
                make.bottom.equalTo(-UIApplication.shared.bottomSafeDistance)
        }
         
        if let path = Bundle.main.path(forResource: "country.json", ofType: nil),
            let data = try? Data.init(contentsOf: URL.init(fileURLWithPath: path)),
            let json = String.init(data: data, encoding: .utf8),
            let array = [CountryModel].deserialize(from: json) as? [CountryModel] {
            self.countryArray = array
        }
        
    }
    
    @objc func cancel() {
        dismissAnimation()
    }
    
    @objc func confirm() {
        let row = self.picker.selectedRow(inComponent: 0)
        self.selectCountry(self.countryArray[row].en)
        dismissAnimation()
    }
    
    func showAnimation() {
        UIApplication.shared.keyWindow?.addSubview(self)
        self.frame = UIScreen.main.bounds
        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.3) {
            self.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
            self.contentView.snp.updateConstraints { (make) in
                make.bottom.equalTo(0)
            }
            self.layoutIfNeeded()
        }
    }
    
    func dismissAnimation() {
        UIApplication.shared.keyWindow?.addSubview(self)
        self.frame = UIScreen.main.bounds
        UIView.animate(withDuration: 0.3, animations: {
            self.backgroundColor = .clear
            self.contentView.snp.updateConstraints { (make) in
                make.bottom.equalTo(self.contentViewHeight)
            }
            self.layoutIfNeeded()
        }) { (_) in
            self.removeFromSuperview()
        }
    }
    
    @objc func tap(_ ges: UIGestureRecognizer) {
        if !self.contentView.frame.contains(ges.location(in: self)) {
            dismissAnimation()
        }
    }
    
    deinit {
        print("CountrySelectView    ---    deinit")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CountrySelectView: UIPickerViewDelegate, UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.countryArray.count
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return self.countryArray[row].en
    }
    
    
}
